
const {ccclass, property} = cc._decorator;

@ccclass
export default class LeftCtrl extends cc.Component {
    /** 方向 */
    public direction:cc.Vec2 = cc.v2(0,0);
    /** 中心点坐标 */
    private centerPos:cc.Vec2 = cc.v2(0,0);
    @property(cc.Node)
    joypoint:cc.Node = null;
    start () {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStartOrMove, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchStartOrMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEndOrCancel, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEndOrCancel, this);
        this.centerPos = cc.v2(this.node.width/2, this.node.height/2);
    };
    onTouchStartOrMove(e:{touch:cc.Touch}) {
        this.direction = e.touch.getLocation().sub(this.centerPos).normalize();
        this.joypoint.position = this.direction.mul(80);
    };
    onTouchEndOrCancel(e:{touch:cc.Touch}) {
        this.direction = cc.v2(0,0);
        this.joypoint.position = this.direction.mul(80);
    };

}
